<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="广告标题" prop="bannerTitle">
        <el-input
          v-model="queryParams.bannerTitle"
          placeholder="请输入广告标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="广告正文" prop="bannerText">
        <el-input
          v-model="queryParams.bannerText"
          placeholder="请输入广告正文"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['console:banner:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['console:banner:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['console:banner:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['console:banner:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bannerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="广告图片" align="center" prop="bannerPic" >
      <template slot-scope="scope">
        <el-image :src="scope.row.bannerPic" style="width: 50px; height: 50px;"/>
      </template>
    </el-table-column>
      <el-table-column label="广告标题" align="center" prop="bannerTitle" />
      <el-table-column label="广告正文" align="center" prop="bannerText" />
      <el-table-column label="广告类型" align="center" prop="bannerType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lifeservice_adverttype" :value="scope.row.bannerType"/>
        </template>
      </el-table-column>
      <el-table-column label="图片跳转链接" align="center" prop="clickUrl" />
      <el-table-column label="广告标签" align="center" :formatter="nameFormatter" prop="tag" />
      <el-table-column label="广告优先级" align="center" prop="priority" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['console:banner:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['console:banner:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改广告配置对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="广告图片" prop="bannerPic">
          <el-input v-model="form.bannerPic" type="text" placeholder="请上传广告图片" disabled/>
          <ImageEdit @changeImage="changeImage" :value="form.bannerPic" :limit="1"></ImageEdit>
        </el-form-item>
        <el-form-item label="广告标题" prop="bannerTitle">
          <el-input v-model="form.bannerTitle" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="广告正文" prop="bannerText">
          <el-input v-model="form.bannerText" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="图片跳转链接" prop="clickUrl">
          <el-input v-model="form.clickUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="广告类型" prop="bannerType">
          <el-select v-model="form.bannerType" placeholder="请选择广告类型" clearable>
            <el-option
              v-for="dict in dict.type.lifeservice_adverttype"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="广告标签" prop="tags">
          <el-select v-model="form.tags" placeholder="请选择广告标签" multiple clearable>
            <el-option
              v-for="dict in dict.type.lifeservice_tag"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="广告优先级" prop="priority">
          <el-input v-model="form.priority" placeholder="请输入广告优先级" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBanner, getBanner, delBanner, addBanner, updateBanner } from "@/api/console/banner";
import ImageEdit from "@/components/ImageEdit/Index";

export default {
  name: "Banner",
  dicts: ['lifeservice_adverttype', 'lifeservice_tag'],
  components: { ImageEdit },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 广告配置表格数据
      bannerList: [],
      titles: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bannerPic: null,
        bannerTitle: null,
        bannerText: null,
        bannerType: null,
        clickUrl: null,
        tag: null,
        tags: null,
        priority: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bannerPic: [
          { required: true, message: "广告图片不能为空", trigger: "blur" }
        ],
        bannerType: [
          { required: true, message: "广告类型1首页顶部广告不能为空", trigger: "change" }
        ],
        clickUrl: [
          { required: true, message: "图片跳转链接不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeImage($event) {
      this.form.bannerPic = $event;
    },
    // 标签格式化
    nameFormatter(row) {
      const tagDict = this.dict.type.lifeservice_tag;
      const tags = JSON.parse(row.tag || []);
      let tagDisplay = "";
      for (const tag of tags) {
        for (const dict of tagDict) {
          if (tag == dict.value) {
            if (tagDisplay == "") {
              tagDisplay = dict.label;
            } else {
              tagDisplay = tagDisplay + "," + dict.label;
            }
            break;
          }
        }
      }
      return tagDisplay;
    },
    /** 查询广告配置列表 */
    getList() {
      this.loading = true;
      listBanner(this.queryParams).then(response => {
        this.bannerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bannerPic: null,
        bannerTitle: null,
        bannerText: null,
        bannerType: null,
        clickUrl: null,
        createTime: null,
        updateTime: null,
        tag: null,
        tags: null,
        priority: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.titles = selection.map(item => item.bannerTitle)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加广告配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBanner(id).then(response => {
        this.form = response.data;
        this.form.bannerType = String(this.form.bannerType);
        if (this.form.tag.indexOf('[') == 0) {
          this.form.tags = JSON.parse(this.form.tag);
        }
        this.open = true;
        this.title = "修改广告配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tag = JSON.stringify(this.form.tags);
          if (this.form.id != null) {
            updateBanner(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBanner(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const titles = row.bannerTitle || this.titles;
      this.$modal.confirm('是否确认删除广告标题为"' + titles + '"的数据项？').then(function() {
        return delBanner(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('console/banner/export', {
        ...this.queryParams
      }, `banner_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
